{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "5.5. 卷积神经网络(LeNet).ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/chongzicbo/Dive-into-Deep-Learning-tf.keras/blob/master/5.5.%20%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C(LeNet).ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xavqrsr75_vg",
        "colab_type": "text"
      },
      "source": [
        "##5.5. 卷积神经网络（LeNet）\n",
        "在“多层感知机的从零开始实现”一节里我们构造了一个含单隐藏层的多层感知机模型来对Fashion-MNIST数据集中的图像进行分类。每张图像高和宽均是28像素。我们将图像中的像素逐行展开，得到长度为784的向量，并输入进全连接层中。然而，这种分类方法有一定的局限性。\n",
        "\n",
        "图像在同一列邻近的像素在这个向量中可能相距较远。它们构成的模式可能难以被模型识别。\n",
        "对于大尺寸的输入图像，使用全连接层容易导致模型过大。假设输入是高和宽均为 1,000 像素的彩色照片（含3个通道）。即使全连接层输出个数仍是256，该层权重参数的形状也是 3,000,000×256 ：它占用了大约3 GB的内存或显存。这会带来过于复杂的模型和过高的存储开销。\n",
        "卷积层尝试解决这两个问题。一方面，卷积层保留输入形状，使图像的像素在高和宽两个方向上的相关性均可能被有效识别；另一方面，卷积层通过滑动窗口将同一卷积核与不同位置的输入重复计算，从而避免参数尺寸过大。\n",
        "\n",
        "卷积神经网络就是含卷积层的网络。本节里我们将介绍一个早期用来识别手写数字图像的卷积神经网络：LeNet [1]。这个名字来源于LeNet论文的第一作者Yann LeCun。LeNet展示了通过梯度下降训练卷积神经网络可以达到手写数字识别在当时最先进的结果。这个奠基性的工作第一次将卷积神经网络推上舞台，为世人所知。\n",
        "\n",
        "###5.5.1. LeNet模型\n",
        "LeNet分为卷积层块和全连接层块两个部分。下面我们分别介绍这两个模块。\n",
        "\n",
        "卷积层块里的基本单位是卷积层后接最大池化层：卷积层用来识别图像里的空间模式，如线条和物体局部，之后的最大池化层则用来降低卷积层对位置的敏感性。卷积层块由两个这样的基本单位重复堆叠构成。在卷积层块中，每个卷积层都使用 5×5 的窗口，并在输出上使用sigmoid激活函数。第一个卷积层输出通道数为6，第二个卷积层输出通道数则增加到16。这是因为第二个卷积层比第一个卷积层的输入的高和宽要小，所以增加输出通道使两个卷积层的参数尺寸类似。卷积层块的两个最大池化层的窗口形状均为 2×2 ，且步幅为2。由于池化窗口与步幅形状相同，池化窗口在输入上每次滑动所覆盖的区域互不重叠。\n",
        "\n",
        "卷积层块的输出形状为(批量大小, 通道, 高, 宽)。当卷积层块的输出传入全连接层块时，全连接层块会将小批量中每个样本变平（flatten）。也就是说，全连接层的输入形状将变成二维，其中第一维是小批量中的样本，第二维是每个样本变平后的向量表示，且向量长度为通道、高和宽的乘积。全连接层块含3个全连接层。它们的输出个数分别是120、84和10，其中10为输出的类别个数。\n",
        "\n",
        "下面我们通过Sequential类来实现LeNet模型。"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "a6Kx1oJjs8t2",
        "colab_type": "code",
        "outputId": "1bdc532a-7d83-4dc5-c511-ff68ba06ee71",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 64
        }
      },
      "source": [
        "import tensorflow as tf\n",
        "from tensorflow import keras\n",
        "from tensorflow.keras import layers,Sequential\n",
        "from tensorflow.data import Dataset\n",
        "from tensorflow.keras import losses,optimizers\n",
        "import time\n",
        "import numpy as np"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/html": [
              "<p style=\"color: red;\">\n",
              "The default version of TensorFlow in Colab will soon switch to TensorFlow 2.x.<br>\n",
              "We recommend you <a href=\"https://www.tensorflow.org/guide/migrate\" target=\"_blank\">upgrade</a> now \n",
              "or ensure your notebook will continue to use TensorFlow 1.x via the <code>%tensorflow_version 1.x</code> magic:\n",
              "<a href=\"https://colab.research.google.com/notebooks/tensorflow_version.ipynb\" target=\"_blank\">more info</a>.</p>\n"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XDSbLZHet_zG",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "tf.enable_eager_execution()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WsS0xO4_uB99",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "net=Sequential([\n",
        "      layers.Convolution2D(filters=6,kernel_size=5,activation='sigmoid'),\n",
        "      layers.MaxPool2D(pool_size=2,strides=2),\n",
        "      layers.Convolution2D(filters=16,kernel_size=5,activation='sigmoid'),\n",
        "      layers.MaxPool2D(pool_size=2,strides=2),\n",
        "      layers.Flatten(),#Flatten会将(batch_size,hight,width,channel)形状的输入转换成(batch_size,hight*width*channel)形状\n",
        "      layers.Dense(120,activation='sigmoid'),\n",
        "      layers.Dense(84,activation='sigmoid'),\n",
        "      layers.Dense(10)          \n",
        "])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Yws_1inE6DWm",
        "colab_type": "text"
      },
      "source": [
        "接下来我们构造一个高和宽均为28的单通道数据样本，并逐层进行前向计算来查看每个层的输出形状。"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "j2tLCskQyyZY",
        "colab_type": "code",
        "outputId": "86442253-09f0-4a73-da69-d0ec5a8d9032",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 161
        }
      },
      "source": [
        "X=tf.random.uniform(shape=(1,28,28,1))\n",
        "for layer in net.layers:\n",
        "  X=layer(X)\n",
        "  print(layer.name,'output shape:\\t',X.shape)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "conv2d output shape:\t (1, 24, 24, 6)\n",
            "max_pooling2d output shape:\t (1, 12, 12, 6)\n",
            "conv2d_1 output shape:\t (1, 8, 8, 16)\n",
            "max_pooling2d_1 output shape:\t (1, 4, 4, 16)\n",
            "flatten output shape:\t (1, 256)\n",
            "dense output shape:\t (1, 120)\n",
            "dense_1 output shape:\t (1, 84)\n",
            "dense_2 output shape:\t (1, 10)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bmV64-9W6Hep",
        "colab_type": "text"
      },
      "source": [
        "可以看到，在卷积层块中输入的高和宽在逐层减小。卷积层由于使用高和宽均为5的卷积核，从而将高和宽分别减小4，而池化层则将高和宽减半，但通道数则从1增加到16。全连接层则逐层减少输出个数，直到变成图像的类别数10。"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4Ot6UoWa6Rj4",
        "colab_type": "text"
      },
      "source": [
        "###5.5.2. 获取数据和训练模型\n",
        "下面我们来实验LeNet模型。实验中，我们仍然使用Fashion-MNIST作为训练数据集。"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TP2Z2mZWzMdW",
        "colab_type": "code",
        "outputId": "3df1cca0-59af-489d-f09a-bdaa1670778a",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 161
        }
      },
      "source": [
        "batch_size=256\n",
        "buffer_size=10000\n",
        "def load_data_fashion_mnist(batch_size,buffer_size):\n",
        "  (x_train,y_train),(x_test,y_test)=keras.datasets.fashion_mnist.load_data()\n",
        "  x_train=x_train[:,:,:,np.newaxis]#将三维张量增加一个channel维\n",
        "  x_test=x_test[:,:,:,np.newaxis]\n",
        "  train_iter=Dataset.from_tensor_slices((x_train,y_train)).map(lambda x,y:(x/255,y)).shuffle(buffer_size).batch(batch_size)\n",
        "  test_iter=Dataset.from_tensor_slices((x_test,y_test)).map(lambda x,y:(x/255,y)).batch(batch_size)\n",
        "  return train_iter,test_iter\n",
        "\n",
        "train_iter,test_iter=load_data_fashion_mnist(batch_size=batch_size,buffer_size=buffer_size)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz\n",
            "32768/29515 [=================================] - 0s 0us/step\n",
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n",
            "26427392/26421880 [==============================] - 0s 0us/step\n",
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n",
            "8192/5148 [===============================================] - 0s 0us/step\n",
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n",
            "4423680/4422102 [==============================] - 0s 0us/step\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wxLRf7eH6ZpV",
        "colab_type": "text"
      },
      "source": [
        "定义损失函数和优化器"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "q2xWsOHx4Pqp",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "lr,num_epochs=0.9,5\n",
        "loss=losses.SparseCategoricalCrossentropy(from_logits=True)\n",
        "optimizer=optimizers.SGD(learning_rate=lr)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IMOdB2S96dY-",
        "colab_type": "text"
      },
      "source": [
        "模型编译和训练"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "n1g44biM4lfE",
        "colab_type": "code",
        "outputId": "6d4605b3-c2cc-4fe4-a861-ba031483f546",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 287
        }
      },
      "source": [
        "net.compile(optimizer=optimizer,loss=loss,metrics=['accuracy'])\n",
        "history=net.fit_generator(train_iter,epochs=num_epochs,validation_data=test_iter)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Epoch 1/5\n",
            "234/235 [============================>.] - ETA: 0s - loss: 2.3188 - acc: 0.1014Epoch 1/5\n",
            "235/235 [==============================] - 38s 160ms/step - loss: 2.3186 - acc: 0.1015 - val_loss: 2.3128 - val_acc: 0.1000\n",
            "Epoch 2/5\n",
            "234/235 [============================>.] - ETA: 0s - loss: 1.5710 - acc: 0.2362Epoch 1/5\n",
            "235/235 [==============================] - 37s 158ms/step - loss: 1.5683 - acc: 0.2375 - val_loss: 1.0616 - val_acc: 0.5677\n",
            "Epoch 3/5\n",
            "234/235 [============================>.] - ETA: 0s - loss: 0.8995 - acc: 0.6156Epoch 1/5\n",
            "235/235 [==============================] - 37s 156ms/step - loss: 0.8985 - acc: 0.6159 - val_loss: 0.8187 - val_acc: 0.7072\n",
            "Epoch 4/5\n",
            "234/235 [============================>.] - ETA: 0s - loss: 0.7218 - acc: 0.7021Epoch 1/5\n",
            "235/235 [==============================] - 37s 157ms/step - loss: 0.7210 - acc: 0.7022 - val_loss: 0.7029 - val_acc: 0.7349\n",
            "Epoch 5/5\n",
            "234/235 [============================>.] - ETA: 0s - loss: 0.6272 - acc: 0.7428Epoch 1/5\n",
            "235/235 [==============================] - 38s 161ms/step - loss: 0.6266 - acc: 0.7428 - val_loss: 0.6226 - val_acc: 0.7598\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SUN54Dq15FJ9",
        "colab_type": "text"
      },
      "source": [
        "5.5.3. 小结\n",
        "* 卷积神经网络就是含卷积层的网络。\n",
        "* LeNet交替使用卷积层和最大池化层后接全连接层来进行图像分类。"
      ]
    }
  ]
}